package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.DotificationPictures;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.UserData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.system.service.IDotificationPicturesService;
import com.ruoyi.system.service.IUserDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/userData")
public class UserDataController extends BaseController
{
    @Autowired
    private IUserDataService userDataService;


    @GetMapping("/list")
    public List<UserData> list(UserData userData)
    {
        List<UserData> list = userDataService.selectList(userData);
        if(list.isEmpty()){
            for (int i = 0; i < 12; i++) {
                UserData userData1 = new UserData();
                userData1.setYear(userData.getYear());
                userData1.setMonth(String.valueOf(i+1));
                list.add(userData1);
            }
        }
        return list;
    }
    @GetMapping("/data")
    @Anonymous
    public AjaxResult data(UserData userData)
    {
        Map<String,Object> map = new HashMap<String,Object>();
        List<UserData> listData = new ArrayList<UserData>();
        if(StringUtils.isNotEmpty(userData.getUserId())){
            List<UserData> list = userDataService.selectList(userData);
            if(list.isEmpty()){
                map.put("incomes", BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP));
                map.put("taxAmounts", BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP));
            }else{
                for (UserData data : list) {
                    // 处理月份字段，在 1 - 9 月前面加上 0
                    String month = data.getMonth();
                    if (month != null && month.length() == 1) {
                        data.setMonth(String.format("%02d", Integer.parseInt(month)));
                    }
                    if(StringUtils.isNotEmpty(data.getTitle())){
                        listData.add(data);
                    }
                }
                map.put("incomes", listData.stream()
                        .map(UserData::getIncome)
                        .filter(income -> income != null)
                        .reduce(BigDecimal.ZERO, BigDecimal::add)
                        .setScale(2, BigDecimal.ROUND_HALF_UP));
                map.put("taxAmounts", listData.stream()
                        .map(UserData::getTaxAmount)
                        .filter(taxAmount -> taxAmount != null)
                        .reduce(BigDecimal.ZERO, BigDecimal::add)
                        .setScale(2, BigDecimal.ROUND_HALF_UP));
            }
        }else{
            map.put("incomes", BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP));
            map.put("taxAmounts", BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP));
        }
        map.put("data",listData);
        return success(map);
    }

    @PostMapping
    public AjaxResult add(@RequestBody UserData userData){
        List<UserData>   list =userData.getData();
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setUserId(userData.getUserId());
        }
        return toAjax(userDataService.save(list));
    }

}
